package com.ruiyou.tb;

import android.graphics.Bitmap;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ProgressBar;

import com.tencent.smtt.sdk.WebView;
import com.tencent.smtt.sdk.WebViewClient;

public class UWebViewClient extends WebViewClient {
    EditText etUrl;
    ProgressBar pb;

    public UWebViewClient(EditText et, ProgressBar pb) {
        etUrl = et;
        this.pb = pb;
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView webView, String url) {
        WebView.HitTestResult htr = webView.getHitTestResult();
        // htr==null处理由于自转向而返回键无效问题
        if (!TextUtils.isEmpty(url) && htr == null) {
            webView.loadUrl(url);
            return true;
        }
        return super.shouldOverrideUrlLoading(webView, url);
    }

    @Override
    public void onPageStarted(WebView webView, String s, Bitmap bitmap) {
        super.onPageStarted(webView, s, bitmap);
        if (etUrl != null) {
            etUrl.setText(s);
        }
        if (pb != null) {
            pb.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onPageFinished(WebView webView, String s) {
        super.onPageFinished(webView, s);
        if (etUrl != null) {
            etUrl.setText(s);
        }
        if (pb != null){
        pb.setVisibility(View.GONE);
        }
    }
}
